package com.sduept.nwld.mobile.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.cim.ledger.total.model.PrimaryDeviceCount;
import com.sduept.nwld.dataserver.manager.cim.PrimaryDeviceCountQueryManager;
import com.sduept.nwld.mobile.manager.MobileUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 一次设备统计Service
 */
@RestController
@RequestMapping("/equipmentStatis/")
public class EquipmentStatisService {
	@Inject
	private MobileUtil mobileUtil;//SQL注入检测类
	
	@Inject
	private PrimaryDeviceCountQueryManager queryM;
	
	
	/**按照调管范围根据年份和电压等级统计一次设备台账（电压等级分为：35，110，220，500，全网）
	 * @param year
	 * @param voltage
	 * @return
	 */

	@GetMapping("/countInfo/{year}/{voltage}")
	public String getEquipmentCountInfo(@PathVariable("year") String year,@PathVariable("voltage") String voltage){
		try {
			year = mobileUtil.SQLInjectCheck(year);
			voltage = mobileUtil.SQLInjectCheck(voltage);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		year = year.replace("年", "");
		voltage = voltage.replace("kV", "").replace("KV", "").replace("kv", "");
		List<PrimaryDeviceCount> list;
		if(voltage.equals("全网")||voltage==null) {
			list = queryM.findDispatchPrimaryDeviceCount(Integer.valueOf(year));
		}else{
			list = queryM.findDispatchPrimaryDeviceCountByYearVoltage(Integer.valueOf(year),new String[] {voltage});
		}
		List<StatisticDeviceDto>  dtoList=getStatisticDeviceDtos(list);
		return JSONObject.toJSONString(dtoList);
		
		/*Map<String, List<EquipmentCountDto>> res = ecm.generateVoltagesCountDto(Integer.valueOf(year));
		List<EquipmentCountDto> dtos = res.get(voltage);
		if (dtos == null) 
			return "";
		
		if (!voltage.equals("全网") && Integer.valueOf(voltage) < 500) {
			for (EquipmentCountDto e : dtos) {
				if (e.getRank().equals("总调")) {
					dtos.remove(e);
					break;
				}
			}
		}
		
		dtos.add(ecm.getSummaryData(dtos));
		return JSONObject.toJSONString(dtos);*/
	}
	
	private List<StatisticDeviceDto> getStatisticDeviceDtos(List<PrimaryDeviceCount> deviceCounts) {
		Map<String,StatisticDeviceDto> map=new HashMap<>();
		for (PrimaryDeviceCount deviceCount : deviceCounts) {
			String unitName=deviceCount.getUnitName();
			StatisticDeviceDto dto=map.get(unitName);
			if(null==dto) {
				dto=new StatisticDeviceDto();
				dto.setUnit(unitName);
				dto.setVoltageLevel(dto.getVoltageLevel());
				map.put(unitName, dto);
			}
			switch(deviceCount.getStatisticType()) {
				case 0:{
					if(deviceCount.getStatisticCount()!=null) {
						dto.setStationCount(dto.getStationCount()+deviceCount.getStatisticCount());
					}
					break;
				}
				case 1:{
					if(deviceCount.getStatisticCount()!=null) {
						dto.setBusbarsectionCount(dto.getBusbarsectionCount()+deviceCount.getStatisticCount());
					}
					break;
				}
				case 2:{
					if(deviceCount.getStatisticCount()!=null) {
						dto.setLineCount(dto.getLineCount()+deviceCount.getStatisticCount());
					}
					break;
				}
				case 3:{
					if(deviceCount.getStatisticCount()!=null) {
						dto.setPowerTransformerCount(dto.getPowerTransformerCount()+deviceCount.getStatisticCount());
					}
					break;
				}
				case 4:{
					if(deviceCount.getStatisticCount()!=null) {
						dto.setBreakerCount(dto.getBreakerCount()+deviceCount.getStatisticCount());
					}
					break;
				}
				case 5:{
					if(deviceCount.getStatisticCount()!=null) {
						dto.setGeneratorCount(dto.getGeneratorCount()+deviceCount.getStatisticCount());
					}
				}
			}
		}
		List<StatisticDeviceDto> dtos=new ArrayList<>(map.values());
		//计算每个单位的合计
		for (StatisticDeviceDto statisticDeviceDto : dtos) {
			statisticDeviceDto.setSummary(getSummary(statisticDeviceDto));
		}
		//汇总
		if(!deviceCounts.isEmpty()) {
			dtos.add(getSummaryDto(dtos));
		}
		return dtos;
	}
	
	private StatisticDeviceDto getSummaryDto(List<StatisticDeviceDto> dtos) {
		StatisticDeviceDto statisticDeviceDto=new StatisticDeviceDto();
		statisticDeviceDto.setUnit("合计");
		for (StatisticDeviceDto dto : dtos) {
			statisticDeviceDto.setStationCount(statisticDeviceDto.getStationCount()+dto.getStationCount());
			statisticDeviceDto.setBusbarsectionCount(statisticDeviceDto.getBusbarsectionCount()+dto.getBusbarsectionCount());
			statisticDeviceDto.setLineCount(statisticDeviceDto.getLineCount()+dto.getLineCount());
			statisticDeviceDto.setPowerTransformerCount(statisticDeviceDto.getPowerTransformerCount()+dto.getPowerTransformerCount());
			statisticDeviceDto.setBreakerCount(statisticDeviceDto.getBreakerCount()+dto.getBreakerCount());
			statisticDeviceDto.setGeneratorCount(statisticDeviceDto.getGeneratorCount()+dto.getGeneratorCount());
			statisticDeviceDto.setSummary(statisticDeviceDto.getSummary()+dto.getSummary());
		}
		return statisticDeviceDto;
	}
	private Long getSummary(StatisticDeviceDto dto) {
		Long c=0l;
		c+=dto.getStationCount();
		c+=dto.getBusbarsectionCount();
		c+=dto.getLineCount();
		c+=dto.getPowerTransformerCount();
		c+=dto.getBreakerCount();
		c+=dto.getGeneratorCount();
		return c;
	}
	
	 public class StatisticDeviceDto  {
		 
			private String unit;
			
			private String voltageLevel;
			
			private long stationCount;
			
			/**
			 * 母线数量
			 */
			private long busbarsectionCount;
			
			private long lineCount;
			
			/**
			 * 变压器数量
			 */
			private long powerTransformerCount;
			
			/**
			 * 断路器
			 */
			private long breakerCount;
			
			/**
			 * 发电机数量
			 */
			private long generatorCount;
			
			/**
			 * 串补
			 */
			private long seriesCompensationCount;
			
			/**
			 * 高抗
			 */
			private long highResistanceCount;
			
			private long summary;
			

			public String getUnit() {
				return unit;
			}

			public void setUnit(String unit) {
				this.unit = unit;
			}

			public String getVoltageLevel() {
				return voltageLevel;
			}

			public void setVoltageLevel(String voltageLevel) {
				this.voltageLevel = voltageLevel;
			}

			public long getStationCount() {
				return stationCount;
			}

			public void setStationCount(long stationCount) {
				this.stationCount = stationCount;
			}

			public long getBusbarsectionCount() {
				return busbarsectionCount;
			}

			public void setBusbarsectionCount(long busbarsectionCount) {
				this.busbarsectionCount = busbarsectionCount;
			}

			public long getLineCount() {
				return lineCount;
			}

			public void setLineCount(long lineCount) {
				this.lineCount = lineCount;
			}

			public long getPowerTransformerCount() {
				return powerTransformerCount;
			}

			public void setPowerTransformerCount(long powerTransformerCount) {
				this.powerTransformerCount = powerTransformerCount;
			}

			public long getBreakerCount() {
				return breakerCount;
			}

			public void setBreakerCount(long breakerCount) {
				this.breakerCount = breakerCount;
			}

			public long getGeneratorCount() {
				return generatorCount;
			}

			public void setGeneratorCount(long generatorCount) {
				this.generatorCount = generatorCount;
			}

			public long getSummary() {
				return summary;
			}

			public void setSummary(long summary) {
				this.summary = summary;
			}

			public long getSeriesCompensationCount() {
				return seriesCompensationCount;
			}

			public void setSeriesCompensationCount(long seriesCompensationCount) {
				this.seriesCompensationCount = seriesCompensationCount;
			}

			public long getHighResistanceCount() {
				return highResistanceCount;
			}

			public void setHighResistanceCount(long highResistanceCount) {
				this.highResistanceCount = highResistanceCount;
			}

			
		}
	
}
